package com.hyopt.common.push.Notification;

import com.hyopt.common.push.config.UmConfig;
import com.hyopt.common.push.util.UmPushUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@Data
public class UmNotification {

    private Map body;
    private String appMasterSecret;
    private String sys;
    private Map params;

    public UmNotification(String type,Map otherParams){
        Map params=new HashMap();
        params.put("timestamp", Integer.toString((int)(System.currentTimeMillis() / 1000)));
        params.put("production_mode", UmConfig.ProductionMode);
        params.put("type",type);
        if(params!=null&&params.isEmpty()){
            params.putAll(otherParams);
        }
        this.setParams(params);
    }

    public void setKeyValue(String key,Object value){
        Map content=this.getBody();
        if(this.getSys().equals(UmConfig.Sys.Android.name())){
            UmPushUtil.setAndriodKeyValue(content,key,value);
        }else if(this.getSys().equals(UmConfig.Sys.IOS.name())){
            UmPushUtil.setIOSKeyValue(content,key,value);
        }
    }

    public void setDescription(String description){
        this.setKeyValue("description",description);
    }

    public void setDeviceTokens(String deviceToken){
        this.setKeyValue("device_tokens",deviceToken);
    }

	// 通知标题
	public void setTitle(String title) {
        this.setKeyValue("title", title);
	}

	public void setIcon(String icon) throws Exception {
        this.setKeyValue("icon", icon);
	}

    public void setBuilderId(Integer builder_id) throws Exception {
        this.setKeyValue("builder_id", builder_id);
	}

	public void setLargeIcon(String largeIcon) throws Exception {
        this.setKeyValue("largeIcon", largeIcon);
	}

    public void setImg(String img) throws Exception {
        this.setKeyValue("img", img);
	}

	//可选，定时发送时间，若不填写表示立即发送。 格式: "yyyy-MM-dd HH:mm:ss"
    public void setStartTime(String startTime) throws Exception {
        this.setKeyValue("start_time", startTime);
    }

//	// 收到通知是否震动,默认为"true"
	public void setPlayVibrate(Boolean play_vibrate){
        this.setKeyValue("play_vibrate", play_vibrate.toString());
	}
//	// 收到通知是否闪灯,默认为"true"
	public void setPlayLights(Boolean play_lights) {
        this.setKeyValue("play_lights", play_lights.toString());
	}
//	// 收到通知是否发出声音,默认为"true"
	public void setPlaySound(Boolean play_sound) {
        this.setKeyValue("play_sound", play_sound.toString());
	}
//	// 通知声音，R.raw.[sound]. 如果该字段为空，采用SDK默认的声音
	public void setSound(String sound) throws Exception {
        this.setKeyValue("sound", sound);
	}
//	// 收到通知后播放指定的声音文件
	public void setPlaySound(String sound) throws Exception {
        this.setPlaySound(true);
        this.setSound(sound);
	}
//
//	// 点击"通知"的后续行为，默认为打开app。
	public void goAppAfterOpen() throws Exception {
        this.setAfterOpenAction(UmConfig.AfterOpenAction.go_app.name());
	}
	public void goUrlAfterOpen(String url) throws Exception {
        this.setAfterOpenAction(UmConfig.AfterOpenAction.go_url.name());
        this.setUrl(url);
	}
	public void goActivityAfterOpen(String activity) throws Exception {
        this.setAfterOpenAction(UmConfig.AfterOpenAction.go_activity.name());
        this.setActivity(activity);
	}
	public void goCustomAfterOpen(String custom) throws Exception {
        this.setAfterOpenAction(UmConfig.AfterOpenAction.go_custom.name());
        this.setCustomField(custom);
	}
	public void goCustomAfterOpen(JSONObject custom) throws Exception {
        this.setAfterOpenAction(UmConfig.AfterOpenAction.go_custom.name());
        this.setCustomField(custom);
	}

    // 点击"通知"的后续行为，默认为打开app。原始接口
	public void setAfterOpenAction(String action) {
        this.setKeyValue("after_open", action);
	}
	public void setUrl(String url) throws Exception {
        this.setKeyValue("url", url);
	}
	public void setActivity(String activity) throws Exception {
        this.setKeyValue("activity", activity);
	}
	public  void setCustomField(String custom){
        this.setKeyValue("custom", custom);
	}
	public  void setCustomField(JSONObject custom) throws Exception {
		this.setKeyValue("custom", custom);
	}

    public void setAlert(Map<String,Object> map) {
        this.setKeyValue("alert", map);
    }

    //content-available
    public void setNoAlert() {
        this.setKeyValue("content-available", 1);
    }

}
